package com.freshmarket.freshmarket.controller;


import com.freshmarket.freshmarket.controller.form.NewOrderByCarForm;
import com.freshmarket.freshmarket.controller.form.NewOrderByLimitedForm;
import com.freshmarket.freshmarket.entity.Pages;
import com.freshmarket.freshmarket.entity.User;
import com.freshmarket.freshmarket.service.impl.OrdersServiceImpl;
import com.freshmarket.freshmarket.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Squirrel
 * @since 2020-09-04
 */
@RestController
@RequestMapping("/freshmarket/orders")
@Slf4j
public class OrdersController {
    final OrdersServiceImpl ordersService;

    public OrdersController(OrdersServiceImpl ordersService) {
        this.ordersService = ordersService;
    }

    @RequestMapping("/newOrderByCar")
    public Result newOrderByCar(@Valid NewOrderByCarForm newOrderByCarForm, HttpSession httpSession) throws Exception {
        if (newOrderByCarForm.getUserId() == 0) {
            Object user = httpSession.getAttribute("user");
            newOrderByCarForm.setUserId(((User) user).getUserId());
        }
        ordersService.newOrderByCar(newOrderByCarForm);
        return Result.successResult();
    }

    @RequestMapping("/newOrderByLimited")
    public Result newOrderByLimited(@Valid NewOrderByLimitedForm newOrderByLimitedForm, HttpSession httpSession) throws Exception {

        if (newOrderByLimitedForm.getUserId() == 0) {
            Object user = httpSession.getAttribute("user");
            newOrderByLimitedForm.setUserId(((User) user).getUserId());
        }
        ordersService.newOrderByLimited(newOrderByLimitedForm);
        return Result.successResult();
    }

    @RequestMapping("/payOrders")
    public Result payOrders(int ordersId)throws Exception{
        ordersService.payOrders(ordersId);
        return Result.successResult();
    }

    @RequestMapping("/rollbackOrders")
    public Result rollbackOrders(int ordersId)throws Exception{
        ordersService.rollbackOrders(ordersId);
        return Result.successResult();
    }

    /**
     *查询个人订单selectPersonOrders
     */
    //条件分页查询
    @RequestMapping("selectPersonOrders")
    public Result selectPersonOrders(@RequestParam(defaultValue = "1")int pageIndex,
                                     @RequestParam(defaultValue = "3")int pageSize,
                                     String ordersStatus, HttpServletRequest request)throws Exception {


        User user = (User) request.getSession().getAttribute("user");
        Result result = ordersService.selectPersonOrdersAll(pageIndex, pageSize, user.getUserId(), ordersStatus);
        return result;
    }

}

